//
// BaseComponent.cs
//
// This file is part of the QuickStart Engine. See http://www.codeplex.com/QuickStartEngine
// for license details.
//

using Microsoft.Xna.Framework;

namespace Alex_s_Dream_The_Toy_World
{
    /// <summary>
    /// Base component class from which all components are derived
    /// </summary>
    public abstract class BaseComponent
    {
        /// <summary>
        /// Entity this component belongs to.
        /// </summary>
        protected BaseEntity parentEntity;

        /// <summary>
        /// Whether or not this component is active. When active this component will have Update()
        /// called on it each frame.
        /// </summary>
        public bool Active
        {
            get { return Active; }
        }
        private bool active;

        /// <summary>
        /// Create a component
        /// </summary>
        /// <param name="parent">Entity this component belongs to.</param>
        public BaseComponent(BaseEntity parent)
        {
            this.parentEntity = parent;

            Initialize();
        }

        /// <summary>
        /// Initialize the component, which will attach it to the entity
        /// </summary>
        public virtual void Initialize()
        {
            this.parentEntity.AddComponent(this);
        }

        /// <summary>
        /// Shutdown the component and remove it from the entity and scene (if needed)
        /// </summary>
        public virtual void Shutdown()
        {

        }

        /// <summary>
        /// Send a message directly to components through this method.
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <returns>Returns true if message was handled</returns>
        public abstract bool ExecuteMessage(IMessage message);

        /// <summary>
        /// Loads all needed ContentManager content.
        /// </summary>
        public virtual void LoadContent()
        {
        }

        /// <summary>
        /// Unloads all ContentManager content.
        /// </summary>
        public virtual void UnloadContent()
        {
        }

        /// <summary>
        /// Update the component
        /// </summary>
        /// <param name="gameTime">XNA Timing snapshot</param>
        public virtual void Update(GameTime gameTime)
        {            
        }

        /// <summary>
        /// Allows the renderer to query this component for render information
        /// </summary>
        /// <param name="desc">Descriptor reference from the renderer</param>
        public virtual void QueryForChunks(ref RenderPassDesc desc)
        {
        }

        /// <summary>
        /// Allows the renderer to query this component for render information
        /// </summary>
        /// <param name="desc">Descriptor reference from the renderer</param>
        public virtual void QueryForSkinnedModelChunks(ref RenderPassDesc desc)
        {
        }

        protected void ActivateComponent()
        {
            if (!this.active)
            {
                this.active = true;

                this.parentEntity.ActivateComponent(this);
            }
        }

        protected void DeactivateComponent()
        {
            if (this.active)
            {
                this.active = false;

                this.parentEntity.DeactivateComponent(this);
            }
        }
    }
}
